# _*_coding:utf-8_*_


from ATS.instrumentDriver import InstCom as inst
from ATS.datamanager import DataManager
import time

OK = 0


# 正常来说应该调用驱动文件中的方法
def sweepsetup(source, sweeplist, parament, power=-20, ifbandwidth=1E4, sweeptype="LIN"):
    start, stop, point = sweeplist
    inst.write(source, "CALC:PAR:COUN %s" % len(parament))
    for i in range(0, len(parament)):
        inst.write(source, "CALC:PAR%s:DEF %s" % (i + 1, parament[i]))
    inst.write(source, "SENS:FREQ:STAR %s" % start)
    inst.write(source, "SENS:FREQ:STOP %s" % stop)
    inst.write(source, "SENS:SWE:POIN %s" % point)
    inst.write(source, "SOUR:POW %s" % power)
    inst.write(source, "SENS:SWE:TYPE %s" % sweeptype)
    inst.write(source, "SENS:BWID %s" % ifbandwidth)
    time.sleep(0.5)


def marker_set(source='VNA1', on_off=True, mknumber=1, mk_x=1e9):
    if on_off:
        inst.write(source, 'CALC1:MARK%s ON' % mknumber)
        inst.write(source, 'CALC1:MARK%s:X %s' % (mknumber, mk_x))
        inst.write(source, 'CALC1:SEL:MARK%s:Y? ' % mknumber)
        rst = inst.read(source)
        return rst
    else:
        inst.write(source, 'CALC1:MARK%s OFF' % mknumber)
        return OK


def mark_read(source, markers, s21_limit=None, s11_limit=None):
    buf = []
    marker_set(on_off=False, mknumber=5)
    marker_set(on_off=False, mknumber=6)

    inst.write(source, 'CALC1:PAR1:SEL')
    for i in range(1, 5):
        str1 = marker_set(source, True, i, markers[i - 1])
        ss = str1.split(',')
        print("ss",ss)
        if is_number(ss[0]):
            buf.append(float(ss[0]))
    print("1111111111111", buf)

    inst.write(source, 'CALC1:PAR2:SEL')
    buf11 = []
    for i in range(1, 5):
        str1 = marker_set(source, True, i, markers[i - 1])
        ss = str1.split(',')
        if is_number(ss):
            buf11.append(float(ss[0]))
    print("2222222222222", buf11)

    s21_min = float(s21_limit[0])
    s21_max = float(s21_limit[1])
    s11_min = float(s11_limit[0])
    s11_max = float(s11_limit[1])

    state21 = []
    for i in buf:
        if is_number(i):
            if s21_max > float(i) > s21_min:
                state21.append(1)
            else:
                state21.append(0)
    print("state21", state21)
    state11 = []
    for i in buf11:
        if is_number(i):
            if s11_max > float(i) > s11_min:
                state11.append(1)
            else:
                state11.append(0)
    print("state11", state11)
    DataManager.get_DM().postarraydata('Frequency', markers)
    DataManager.get_DM().postarraydata('S21_Ampl', buf)
    DataManager.get_DM().postarraydata('S21_Pass', state21)
    DataManager.get_DM().postarraydata('S11_Ampl', buf11)
    DataManager.get_DM().postarraydata('S11_Pass', state11)


# 判断是否为数字
def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass
    return False


def VNA_TEST(vna):
    # start_t = time.clock()
    vna_obj = None
    output_result = {}
    vna_obj = inst.getinstobj(vna)
    vna_obj.marker_on()
    raw_xlist = vna_obj.export_xdata()
    raw_ydata = vna_obj.export_ydata()
    raw_ylist = []
    index = 0
    for index, ss in enumerate(raw_ydata):
        if index % 2 == 0:
            raw_ylist.append(raw_ydata[index])
    #    a = {}
    #    a['Frequency']=raw_xlist
    #    a['Amplitude']=raw_ylist
    #    print(a)
    #    postAllDataForOneLoop(a)

    postarraydata('Frequency', raw_xlist)
    postarraydata('Amplitude', raw_ylist)
    print(raw_xlist)
    print(raw_ylist)
    return OK


def ENA_Eval(vna):
    vna_obj = None
    vna_obj = inst.getinstobj(vna)
    raw_xlist = vna_obj.export_xdata()
    raw_ylist = vna_obj.export_ydata()
    print(raw_xlist)
    print(raw_ylist)

    s1 = vna_obj.marker_set(on_off=True, mknumber=1, mk_x=3e009)
    print('value_marker1 = %s' % s1)
    vna_obj.marker_set(on_off=True, mknumber=2, mk_x=6e009)
    s1 = vna_obj.marker_set(on_off=True, mknumber=3)
    print('value_marker3 = %s' % s1)
    vna_obj.marker_set(on_off=True, mknumber=4)
    #    postarraydata('p1_o1',[1,2,3,4,5])

    postarraydata('Frequency', raw_xlist)
    print('x OK')
    postarraydata('Amplitude', raw_ylist)
    print('y OK')
